﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Inicio.Entities;

namespace Inicio.Forms.Contacto
{
    public partial class AddContact : Form
    {
        public List<Control> myControls = new List<Control>();

        public AddContact()
        {
            InitializeComponent();
        }

        private void AddContact_Load(object sender, EventArgs e)
        {
            CargoTextos();
            CargoComboBox(comboBox1);

            myControls.Add(textBox1);
            myControls.Add(textBox2);
            myControls.Add(textBox3);
            //TODO: Ver como incorporar el combo a el control
            //myControls.Add(comboBox1);
            ShowErrors();

        }

        private void CargoComboBox(ComboBox comboBox1)
        {
            Core.Connection conn = new Core.Connection();

            List<Obra> dataBind = new List<Obra>();

            Obra defaultObra = new Obra(0, "Seleccione una Obra");
            //Obra defaultObra = new Obra();
            dataBind.Add(defaultObra);
            dataBind.AddRange(conn.LoadObras());
            
            comboBox1.ValueMember = "obraId";
            comboBox1.DisplayMember = "obraName";
            comboBox1.DataSource = dataBind;
        }

        private void CargoTextos()
        {
            label1.Text = "Agregar Contacto";
            label2.Text = "Nombres";
            label3.Text = "Apellido";
            label4.Text = "Celular";
            label5.Text = "Celular Alternativo";
            label6.Text = "Telefono";
            label7.Text = "Obra";
            label8.Text = "E-mail";
            label9.Text = "Informacion Adicional";

            button1.Text = "Agregar";
            button2.Text = "Cerrar";
        }

        private void ShowErrors()
        {
            string test = "";

            test += SetErrorFormat(textBox1);
            test += SetErrorFormat(textBox2);
            test += SetErrorFormat(textBox3);
            
            ValidateButton(test);
        }

        private string SetErrorFormat(Control control)
        {
            string res;

            if (errorProvider1.GetError(control) != "")
            {
                res = string.Format("- {0} \n", errorProvider1.GetError(control));
            }
            else
                res = "";
            return res;
        }

        private void ValidateButton(string test)
        {
            if (test == "" && myControls.Where(x => x.Text.Length == 0).Count() == 0)
            {
                button1.Enabled = true;
            }
            else
                button1.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            LengthValidation((TextBox)sender, 20, "Se deben ingresar hasta {0} caracteres y no ser un valor nulo");
        }

        private void LengthValidation(TextBox textbox, int max, string msg)
        {
            if (textbox.Text.Length > max || textbox.Text.Length == 0)
            {
                errorProvider1.SetError(textbox, string.Format(msg, max));
            }
            else
                errorProvider1.SetError(textbox, null);
            ShowErrors();
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsSymbol(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten numeros");
                e.Handled = true;
                return;
            }
        }


    }
}
